<template>
  <Dialog v-model="dialogVisible" :title="dialogTitle" width="50%">
    <el-form
      ref="formRef"
      v-loading="formLoading"
      :model="formData"
      :rules="formRules"
      label-width="150px"
    >
      <el-row>
        <el-col :span="14">
          <el-form-item label="规则名称 :" prop="name">
            <el-input v-model="formData.name" placeholder="请输入规则名称" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="14">
          <el-form-item label="是否启用 :" prop="name">
            <el-switch v-model="formData.status" :active-value="0" :inactive-value="1" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="22">
          <el-form-item label="描述 :">
            <el-input v-model="formData.description" placeholder="请输入内容" type="textarea" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-divider /><el-text class="mx-1" size="large"><h4>规则设置</h4></el-text>
      <el-row>
        <el-col :span="14">
          <el-form-item label="元数据完整性占比 :" prop="metadata_ratio">
            <!-- <el-input
              v-model="formData.metadata_ratio"
              placeholder="请输入百分比整数 例：100%"
              type="number"
            /> -->
            <el-input-number
              v-model="formData.metadata_ratio"
              :min="0"
              controls-position="right"
              placeholder="请输入百分比整数 例：100%"
            />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="14">
          <el-form-item label="资源状态占比 :" prop="resource_state_ratio">
            <el-input-number
              v-model="formData.resource_state_ratio"
              :min="0"
              controls-position="right"
              placeholder="请输入百分比整数 例：100%"
            />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="14">
          <el-form-item label="数据加工占比 :" prop="data_process_ratio">
            <el-input-number
              v-model="formData.data_process_ratio"
              :min="0"
              controls-position="right"
              placeholder="请输入百分比整数 例：100%"
            />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <template #footer>
      <el-button :disabled="formLoading" type="primary" @click="submitForm">确 定</el-button>
      <el-button @click="dialogVisible = false">取 消</el-button>
    </template>
  </Dialog>
</template>
<script lang="ts" setup>
import { CommonStatusEnum } from '@/utils/constants'
import * as RuleManage from '@/api/rule/index'

const { t } = useI18n() // 国际化
const message = useMessage() // 消息弹窗

const dialogVisible = ref(false) // 弹窗的是否展示
const dialogTitle = ref('') // 弹窗的标题
const formLoading = ref(false) // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
const formType = ref('') // 表单的类型：create - 新增；update - 修改
const formData = ref({
  name: '',
  id: '',
  description: '',
  status: CommonStatusEnum.ENABLE,
  metadata_ratio: 0,
  resource_state_ratio: 0,
  data_process_ratio: 0
})

const formRules = reactive({
  name: [{ required: true, message: '规则名称不能为空', trigger: 'blur' }],
  metadata_ratio: [
    {
      required: true,
      pattern: /^([0-9][0-9]{0,1}|100)$/,
      message: '请输入正确的格式{0-100},',
      trigger: 'blur'
    }
  ],
  resource_state_ratio: [
    {
      required: true,
      pattern: /^([0-9][0-9]{0,1}|100)$/,
      message: '请输入正确的格式{0-100},',
      trigger: 'blur'
    }
  ],
  data_process_ratio: [
    {
      required: true,
      // pattern: /^\d+(\.\d+)?%$/,
      pattern: /^([0-9][0-9]{0,1}|100)$/,
      message: '请输入正确的格式{0-100},',
      trigger: 'blur'
    }
  ]
})
const formRef = ref() // 表单 Ref

/** 打开弹窗 */
const open = async (type: string, id?: number) => {
  dialogVisible.value = true
  dialogTitle.value = t('action.' + type)
  formType.value = type
  resetForm()
  // 修改时，设置数据
  if (id) {
    formLoading.value = true
    try {
      formData.value = await RuleManage.getRuleManage(id)
      console.log('---------------formData.value', formData.value)
    } finally {
      formLoading.value = false
    }
  }
}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗

/** 提交表单 */
const emit = defineEmits(['success']) // 定义 success 事件，用于操作成功后的回调
const submitForm = async () => {
  // 校验表单
  if (!formRef) return
  const valid = await formRef.value.validate()
  if (!valid) return
  // 提交请求
  formLoading.value = true
  try {
    const data = formData.value as unknown as RuleManage.RuleManageVO
    if (formType.value === 'create') {
      await RuleManage.createRuleManage(data)
      message.success(t('common.createSuccess'))
    } else {
      await RuleManage.updateRuleManage(data)
      message.success(t('common.updateSuccess'))
    }
    dialogVisible.value = false
    // 发送操作成功的事件
    emit('success')
  } finally {
    formLoading.value = false
  }
}

/** 重置表单 */
const resetForm = () => {
  formData.value = {
    id: undefined,
    name: '',
    status: CommonStatusEnum.ENABLE,
    description: '',
    metadata_ratio: 0,
    resource_state_ratio: 0,
    data_process_ratio: 0
  }
  formRef.value?.resetFields()
}

/** 增加占比 */
const increasePercentage = (value: String) => {
  switch (value) {
    case 'metadata_ratio':
      formData.value.metadata_ratio = formData.value.metadata_ratio + 1
      break
    case 'resource_state_ratio':
      formData.value.resource_state_ratio = formData.value.resource_state_ratio + 1
      break
    case 'data_process_ratio':
      formData.value.data_process_ratio = formData.value.data_process_ratio + 1
      break
  }
}

/** 减少占比 */
const decreasePercentage = (value) => {
  switch (value) {
    case 'metadata_ratio':
      formData.value.metadata_ratio = formData.value.metadata_ratio - 1
      break
    case 'resource_state_ratio':
      formData.value.resource_state_ratio = formData.value.resource_state_ratio - 1
      break
    case 'data_process_ratio':
      formData.value.data_process_ratio = formData.value.data_process_ratio - 1
      break
  }
}
</script>
